import store from '@/store'
import axios from 'axios'
import { Message } from 'element-ui'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 4000
})

// 拦截器
service.interceptors.request.use(config => {
  if (store.getters.token) {
    config.headers.token = `${store.getters.token}`
  }
  return config
}

)
// 响应拦截器
service.interceptors.response.use(response => {
  // 请求成功
  if (response.data.isSuccess && response.config.method === 'post') {
    return response.data
  } else if (response.config.method === 'get') {
    return response
  } else {
    Message.error(response.data.msg)
    return Promise.reject(response.data.msg)
  }
},
error => {
  Message.error('系统繁忙，请稍后重试')
  return Promise.reject(error)
}
)

export default service
